package com.aspire.moss.portal.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

import com.aspire.moss.portal.R;
import com.aspire.moss.portal.view.Workspace.WorkspaceListener;

public class ClipImageView extends View implements WorkspaceListener {
	
	private Bitmap clipnofocus , clipfocus; //当前页的选中与非选中的图像
	
	private transient int  mCount ;//有多少个
	
	private transient int mWidth , mHeight;
	
	private transient int mSelect;//哪个选中
	
	private float clipPadding; // 间隔
	
	public ClipImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs , R.styleable.ClipImageView);
		clipfocus = ((BitmapDrawable)a.getDrawable(R.styleable.ClipImageView_clipfocus)).getBitmap();
		clipnofocus = ((BitmapDrawable)a.getDrawable(R.styleable.ClipImageView_clipnofocus)).getBitmap();
		clipPadding = a.getDimension(R.styleable.ClipImageView_clipPadding, 3f);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		mWidth = MeasureSpec.getSize(widthMeasureSpec);
		mHeight = MeasureSpec.getSize(heightMeasureSpec);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		final int childsWidths = (clipfocus.getWidth() * mCount);
		final int childsTop = (mHeight - clipfocus.getHeight()) / 2 ;
		final int releft = (mWidth - childsWidths) / 2 ;
		for(int i = 0 ; i < mCount ; i++){
			int left = releft + i *  clipnofocus.getWidth();
			if(i > 0){
				left += clipPadding;
			}
			if(mSelect == i){
				canvas.drawBitmap(clipfocus, left, childsTop, null);
			}else{
				canvas.drawBitmap(clipnofocus, left, childsTop, null);
			}
		}
		canvas.save();
	}

	public void onSelectChange(int select){
		if(this.mSelect != select){
			this.mSelect = select;
			postInvalidate();
		}
	}
	
	@Override
	public void onSelectScreenChange(int mCurrentScreen) {
		onSelectChange(mCurrentScreen);
	}

	@Override
	public void onScreenCountChange(int count) {
		this.mCount = count;
		postInvalidate();
	}
	
	interface ClipImageViewOnSelectChang{
		
		void onScreenChange(int screen);
		
		void onSelectChamge(int select);
		
	}
}
